﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace XnaAux
{
    public class AnimationClipsReader : ContentTypeReader<AnimationClips>
    {
        protected override AnimationClips Read(ContentReader aInput, AnimationClips aExistingInstance)
        {
            AnimationClips lClips = new AnimationClips();

            // Determine how many clips there are.
            int lClipCount = aInput.ReadInt32();

            lClips.SkelToBone = aInput.ReadObject<List<int>>();
            for (int c = 0; c < lClipCount; c++)
            {
                // Create a clip and load it up
                AnimationClips.Clip lClip = new AnimationClips.Clip();
                lClip.Name = aInput.ReadString();
                lClip.Duration = aInput.ReadDouble();

                // Determine how many bones there are
                int lBoneCnt = aInput.ReadInt32();
                lClip.Keyframes = new List<AnimationClips.Keyframe>[lBoneCnt];

                for (int i = 0; i < lBoneCnt; i++)
                {
                    // Determine how many keyframes there are.
                    int lCnt = aInput.ReadInt32();
                    List<AnimationClips.Keyframe> lBoneKeyframes = new List<AnimationClips.Keyframe>(lCnt);
                    lClip.Keyframes[i] = lBoneKeyframes;

                    for (int j = 0; j < lCnt; j++)
                    {
                        AnimationClips.Keyframe lKeyframe = new AnimationClips.Keyframe();
                        lKeyframe.Time = aInput.ReadDouble();
                        lKeyframe.Rotation = aInput.ReadQuaternion();
                        lKeyframe.Translation = aInput.ReadVector3();

                        lBoneKeyframes.Add(lKeyframe);
                    }
                }
                lClips.Clips[lClip.Name] = lClip;
            }

            return lClips;
        }
    }
}